####
## Project:            Do Corporate Regulations Deter or Stimulate Investment? The Effect of the OECD Anti-Bribery Convention on FDI
## File name:          04-ratification_analysis.R
## Description:        Analyses country-level ratification of the OECD Convention
## Programmed by:      Lorenzo Crippa
####

# Setup ----
rm(list=ls())
setwd("") # INSERT THE REPLICATION FOLDER FILE PATH HERE 
graphics.off()

library(tibble)
library(tidyverse)
library(showtext)
library(modelsummary)
library(fixest)
library(survival)

# plots' font:
font_add("LM Roman 10", "lmroman10-regular.otf")
showtext_auto()

# simplify modelsummary LaTeX format:
options("modelsummary_format_numeric_latex" = "plain")

theme <- theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
               panel.background = element_rect(fill = "transparent", color = NA),
               plot.background = element_rect(fill = "transparent", color = NA),
               axis.line = element_line(colour = "black", linewidth = 0.3),
               text = element_text(colour = "black", size = 14, family = "LM Roman 10"))
theme_set(theme)

# Import data ----
grid <- read_rds("data/ratification_OLS.rds")
surv <- read_rds("data/ratification_surv.rds")

# Model of ratification (exclude US) ----
## OLS ----
mod1 <- feols(oecdRatif ~ l.fdigdpHome | isocodeHome + year,
              cluster = ~isocodeHome,
              data = grid %>%
                filter(isocodeHome != 'US') %>%
                mutate(across(.cols = 5:ncol(.), .fns = ~scale(.x))))
summary(mod1)

mod2 <- feols(oecdRatif ~ l.fdigdpHome + l.lgdpHome + l.gdpcpHome + l.gdpgrowHome +
                l.tradegdpHome + l.ljiHome + l.pcHome | isocodeHome + year,
              cluster = ~isocodeHome,
              data = grid %>%
                filter(isocodeHome != 'US') %>%
                mutate(across(.cols = 5:ncol(.), .fns = ~scale(.x))))

summary(mod2)

mod3 <- feols(oecdRatif ~ l.flows_1 + l.flows_2 + l.flows_3 | isocodeHome + year,
              cluster = ~isocodeHome,
              data = grid %>%
                filter(isocodeHome != 'US') %>%
                mutate(across(.cols = 5:ncol(.), .fns = ~scale(.x))))
summary(mod3)

mod4 <- feols(oecdRatif ~ l.flows_1 + l.flows_2 + l.flows_3 +
                l.fdigdpHome + l.lgdpHome + l.gdpcpHome + l.gdpgrowHome +
                l.tradegdpHome + l.ljiHome + l.pcHome | isocodeHome + year,
              cluster = ~isocodeHome,
              data = grid %>%
                filter(isocodeHome != 'US') %>%
                mutate(across(.cols = 5:ncol(.), .fns = ~scale(.x))))

summary(mod4)

## Cox PH ----
cox1 <- coxph(Surv(time = surv$duration[surv$isocodeHome != 'US'], 
                   event = surv$status[surv$isocodeHome != 'US'],
                   type = 'right') ~ l.fdigdpHome, 
              data = surv %>%
                filter(isocodeHome != 'US') %>%
                mutate(across(.cols = 5:ncol(.), .fns = ~scale(.x))))
summary(cox1)

cox2 <- coxph(Surv(time = surv$duration[surv$isocodeHome != 'US'], 
                   event = surv$status[surv$isocodeHome != 'US'],
                   type = 'right') ~ l.fdigdpHome + l.lgdpHome + l.gdpcpHome + l.gdpgrowHome +
                l.tradegdpHome + l.ljiHome + l.pcHome, 
              data = surv %>%
                filter(isocodeHome != 'US') %>%
                mutate(across(.cols = 5:ncol(.), .fns = ~scale(.x))))
summary(cox2)
cox.zph(cox2)

cox3 <- coxph(Surv(time = surv$duration[surv$isocodeHome != 'US'], 
                   event = surv$status[surv$isocodeHome != 'US'],
                   type = 'right') ~ l.flows_1 + l.flows_2 + l.flows_3, 
              data = surv %>%
                filter(isocodeHome != 'US') %>%
                mutate(across(.cols = 5:ncol(.), .fns = ~scale(.x))))
summary(cox3)

cox4 <- coxph(Surv(time = surv$duration[surv$isocodeHome != 'US'], 
                   event = surv$status[surv$isocodeHome != 'US'],
                   type = 'right') ~ l.flows_1 + l.flows_2 + l.flows_3 +
                l.fdigdpHome + l.lgdpHome + l.gdpcpHome + l.gdpgrowHome +
                l.tradegdpHome + l.ljiHome + l.pcHome, 
              data = surv %>%
                filter(isocodeHome != 'US') %>%
                mutate(across(.cols = 5:ncol(.), .fns = ~scale(.x))))
summary(cox4)

## Appendix tables  ----
cf_map <- c('l.flows_1' = 'Home FDI in low-corruption countries (GDP perc.)',
            'l.flows_2' = 'Home FDI in mid-corruption countries (GDP perc.)',
            'l.flows_3' = 'Home FDI in high-corruption countries (GDP perc.)',
            'l.fdigdpHome' = 'Home FDI (GDP perc.)',
            'l.tradegdpHome' = 'Home Trade (GDP perc.)',
            'l.lgdpHome' = 'Home GDP (log)',
            'l.gdpcpHome' = 'Home GDP per capita',
            'l.gdpgrowHome' = 'Home GDP Growth (perc.)',
            'l.ljiHome' = 'Home Judiciary Indep.',
            'l.pcHome' = 'Home POLCON III'
)

### Table B.1 ----
rows <- tribble(~term, ~mod1, ~mod2, ~mod3, ~mod4,
                'Country FE', 'Yes', 'Yes', 'Yes', 'Yes',
                'Year FE', 'Yes', 'Yes', 'Yes', 'Yes',
                'Country-clustered Std. Err.', 'Yes', 'Yes', 'Yes', 'Yes')

attr(rows, 'position') <- c(21, 22, 23)

modelsummary(list('(1)' = mod3, 
                  '(2)' = mod1,
                  '(3)' = mod2,
                  '(4)' = mod4),
             coef_map = cf_map,
             gof_map = c("nobs", "r.squared"),
             add_rows = rows,
             stars = TRUE,
             title = 'Economic determinants of ratification of the OECD Convention. Linear models',
             output = "tables/supp_tab_b1.tex")

### Table B.2 ----
gm <- tribble(
  ~raw,             ~clean,      ~fmt,
  "nobs",           "N",         0,
  "logLik",         "Log.Lik.",  2,
  "statistic.wald", "Wald Chi2", 2)


modelsummary(list('(1)' = cox3, 
                  '(2)' = cox1,
                  '(3)' = cox2,
                  '(4)' = cox4),
             coef_map = cf_map,
             gof_map = gm,
             title = 'Economic determinants of ratification of the OECD Convention. Cox PH models',
             stars = TRUE,
             output = "tables/supp_tab_b2.tex"
)

#====# The End #====#